using MediatR;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;

namespace ChikovMF.Application.Features.Images.Processing;

public class ImageProcessingCommandHandler : IRequestHandler<ImageProcessingCommand, Stream>
{
    public async Task<Stream> Handle(ImageProcessingCommand request, CancellationToken cancellationToken)
    {
        using (var processedImage = Image.Load(request.Stream))
        {
            processedImage.Mutate(x => x.Resize(request.ResizeOptions));

            var stream = new MemoryStream();

            await processedImage.SaveAsJpegAsync(stream, cancellationToken);
            stream.Seek(0, SeekOrigin.Begin);

            return stream;
        }
    }
}
